package com.doyled.jump.object;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.LinkedList;

import com.doyled.jump.IGame.GameInterface;
import com.doyled.jump.animation.Animation;
import com.doyled.jump.animation.TextAnimation;
import com.doyled.jump.framework.ResourceManager;
import com.doyled.jump.util.Drawing;

public class InfoPanel implements GameInterface {

	private BufferedImage image;
	private String turnText;
	private String staticText;
	private int x, y;
	private Font defaultFont;
	private LinkedList<TextAnimation> textAnimations;

	public TextAnimation getLastAnimation() {
		return textAnimations.getLast();
	}

	public InfoPanel(int x, int y) {
		this.x = x;
		this.y = y;
		defaultFont = new Font("Arial", Font.BOLD, 30);
		image = ResourceManager.getInstance().getInfoPanelImage();
		this.textAnimations = new LinkedList<TextAnimation>();
		this.staticText = "";
		this.turnText = "";
	}

	private TextAnimation getFirstAnimation() {
		if (!textAnimations.isEmpty()) {
			return textAnimations.getFirst();
		} else {
			return null;
		}
	}

	public void setAnimationInfoText(String text, int time) {
		TextAnimation animation = new TextAnimation();
		animation.setAnimation(time, text);
		this.textAnimations.add(animation);
	}
	
	public boolean setStaticInfoText(String text) {
		if(!this.staticText.equals(text)){
			this.staticText = text;
			return true;
		}
		return false;
	}
	
	public void setTurnText(String text) {
		this.turnText = text;
	}

	@Override
	public void tick() {
		Animation animation = getFirstAnimation();
		if(animation == null) return;	//return immediately when no animation is exist
		if (animation.isAnimating() == false) {
			textAnimations.remove(0);	//remove the animated animation
		}
	}

	@Override
	public void render(Graphics g) {
		Drawing.draw(g, image, x, y, 400, 40);

		TextAnimation animation = getFirstAnimation();
		String infoText = turnText;
		if (animation != null) {
			infoText += animation.getText();
		} else {
			infoText += staticText;
		}
		int width = g.getFontMetrics(defaultFont).stringWidth(infoText);
		int height = g.getFontMetrics(defaultFont).getHeight();
		int realX = x - width / 2;
		int realY = y + height / 2 - 5;
		g.setColor(Color.WHITE);
		g.setFont(defaultFont);
		g.drawString(infoText, realX, realY);
	}

}
